//
//  XJSecKillGoodsDetailHeaderView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/18.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJSecKillGoodsDetailHeaderView.h"
#import <SDCycleScrollView.h>
#import "UILabel+CountDown.h"

@interface XJSecKillGoodsDetailHeaderView()
{
    NSInteger _cntNum;
}
/**7-18结束时间用于做倒计时*/
@property (nonatomic, copy) NSString *endTimeStamp;

/**轮播图*/
@property(nonatomic, strong) SDCycleScrollView *cycleSrollView;

/**轮播图下面 商品信息 背景view*/
@property(nonatomic, strong) UIView *goodsInfoBgView;
@property(nonatomic, strong) UILabel *nameLb;

@property(nonatomic, strong) UIView *smallbgView; //找个<div>￥56 ￥67 0人付款</div>装
/**价格*/
@property(nonatomic, strong) UILabel *priceLb;
/**原价*/
@property(nonatomic, strong) UILabel *originPriceLb;
/**付款人数*/
@property(nonatomic, strong) UILabel *payNumberLb;
/**7-18倒计时lb*/
@property (nonatomic, strong) UILabel *countDownLb;

@property (nonatomic, strong) NSTimer *xjSecKillGoodsDetailHeaderTimer;
@end


@implementation XJSecKillGoodsDetailHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        
         self.xjSecKillGoodsDetailHeaderTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    }
    return self;
}
#pragma mark - 二 - 2 定时器方法
- (void)timeAction {
 
    [self countDown];
    
    [self.countDownLb setupCountDownTitle:[self currentTimeString] height:30];
}


- (void)countDown {
    
    _cntNum = [self theInteverBetweenCurrentTimeAndEndTime];
    _cntNum --;
}

- (NSString *)currentTimeString {
    if (_cntNum <= 0) {
        return @"00:00:00";
    }else {
        NSString *ctStr = [NSString stringWithFormat:@"  %02ld:%02ld:%02ld后结束" ,_cntNum/3600 ,_cntNum%3600/60 ,_cntNum%60];
        return ctStr;
    }
}

#pragma mark - 二 Private mehtod

/**结束时间 - 系统时间的时间差*/
- (NSInteger)theInteverBetweenCurrentTimeAndEndTime {
    
    NSInteger c = [self currentTimeStamp];
    
    NSInteger e = [self.endTimeStamp integerValue];
    
    //再将时间转换为秒
    return (e-c) / 1000;
}

/**系统当前时间的时间戳*/
- (NSInteger)currentTimeStamp {
    
    NSInteger timeStamp = [[NSDate date] timeIntervalSince1970];
    
    //Java 的 时间戳单位好像是毫秒。这里保持单位一致
    return timeStamp *1000;
}



#pragma mark - 一 赋值
- (void)configRBGoodsDetailHeaderViewImages:(NSArray *)imageArr name:(NSString *)name price:(NSString *)price originPrice:(NSString *)yuanjia payCount:(NSString *)payNum endTimeStamp:(NSString *)endtime {
    
    [self.cycleSrollView setImageURLStringsGroup:imageArr];
    
    self.nameLb.text = [NSString stringWithFormat:@"%@",name];
    self.priceLb.text = [NSString stringWithFormat:@"￥%.2f",[price floatValue]];
    
    //添加中划线
    NSString *originPriceStr = [NSString stringWithFormat:@"￥%.2f",[yuanjia floatValue]];
    self.originPriceLb.attributedText = [self zhongHuaXianWithStr:originPriceStr];
    self.payNumberLb.text = [NSString stringWithFormat:@"%d人付款",[payNum intValue]];
    
    
    //备份倒计时时间
    self.endTimeStamp = endtime;
    
    //7-18倒计时时间
    [self.countDownLb setupCountDownTitle:@"   00:00:00" height:30];
}

//添加中划线

- (NSAttributedString *)zhongHuaXianWithStr:(NSString  *)priceString {
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:priceString attributes:attribtDic];
    
    return attribtStr;
}


- (void)setupUI{
    [self addSubview:self.cycleSrollView];
    [self.cycleSrollView addSubview:self.countDownLb]; //秒杀倒计时lb
    
    [self addSubview:self.goodsInfoBgView];
    [self.goodsInfoBgView addSubview:self.nameLb];
    
    [self.goodsInfoBgView addSubview:self.smallbgView];
    
    
    // 这三个控件用一个div装
    [self.smallbgView addSubview:self.priceLb];
    [self.smallbgView addSubview:self.originPriceLb];
    [self.smallbgView addSubview:self.payNumberLb];
    
}
#pragma mark - 三 getter
- (SDCycleScrollView *)cycleSrollView {
    if (!_cycleSrollView) {
        _cycleSrollView = [[SDCycleScrollView alloc] init];
        _cycleSrollView.backgroundColor = [UIColor whiteColor];
    }
    return _cycleSrollView;
}
- (UIView *)goodsInfoBgView {
    if (!_goodsInfoBgView) {
        _goodsInfoBgView = [[UIView alloc] init];
    }
    return _goodsInfoBgView;
}
- (UILabel *)nameLb{
    if (!_nameLb) {
        _nameLb = [[UILabel alloc] init];
        _nameLb.numberOfLines = 0;
    }
    return _nameLb;
}

- (UIView *)smallbgView {
    if (!_smallbgView) {
        _smallbgView = [[UIView alloc] init];
    }
    return _smallbgView;
}

- (UILabel *)priceLb {
    if (!_priceLb) {
        _priceLb = [[UILabel alloc] init];
        _priceLb.textColor = [UIColor redColor];
    }
    return _priceLb;
}

- (UILabel *)originPriceLb {
    if (!_originPriceLb) {
        _originPriceLb = [[UILabel alloc] init];
        _originPriceLb.textColor = [UIColor lightGrayColor];
        _originPriceLb.font = [UIFont systemFontOfSize:13.0];
    }
    return _originPriceLb;
}

- (UILabel *)payNumberLb {
    if (!_payNumberLb) {
        _payNumberLb = [[UILabel alloc] init];
        _payNumberLb.textColor = [UIColor lightGrayColor];
        _payNumberLb.font = [UIFont systemFontOfSize:13.0];
    }
    return _payNumberLb;
}

- (UILabel *)countDownLb {
    if (!_countDownLb) {
        _countDownLb = [UILabel new];
        _countDownLb.text = @"00:00:00";
        _countDownLb.textAlignment = NSTextAlignmentCenter;
        
    }
    return _countDownLb;
}

#pragma mark - 四 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 宽高固定(频宽、375)
    [self.cycleSrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(ADAPTHeight(375));
    }];
    [self.countDownLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.cycleSrollView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(200, 50));
        make.bottom.equalTo(self.cycleSrollView).offset(-30);
    }];
    
    // (屏幕宽,100)
    [self.goodsInfoBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.cycleSrollView.mas_bottom);
        make.height.mas_equalTo(ADAPTHeight(60));
    }];
    
    [self.nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsInfoBgView.mas_left).offset(10);
        make.top.equalTo(self.goodsInfoBgView.mas_top).offset(10);
        make.height.mas_equalTo(ADAPTHeight(20));
        make.width.mas_equalTo(ADAPTWidth(200));
    }];
    
    [self.smallbgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLb.mas_bottom).offset(5);
        make.left.bottom.right.equalTo(self.goodsInfoBgView);
    }];
    
    
    [self.priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.smallbgView.mas_left).offset(10);
        make.top.bottom.equalTo(self.smallbgView);
        make.width.mas_equalTo(ADAPTWidth(100));
    }];
    
    [self.originPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.smallbgView);
        make.left.equalTo(self.priceLb.mas_right).offset(10);
        make.width.mas_equalTo(ADAPTWidth(100));
    }];
    
    [self.payNumberLb mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.right.equalTo(self.smallbgView);
        make.width.mas_equalTo(ADAPTWidth(80));
    }];
}

#pragma mark - 五 dealloc
- (void)dealloc {
    XJLog(@"视图dealloc 销毁定时器");
    [self.xjSecKillGoodsDetailHeaderTimer invalidate];
    self.xjSecKillGoodsDetailHeaderTimer = nil;
}
@end
